package com.igeek.boot.controller;


import com.igeek.boot.result.Result;
import com.igeek.boot.service.IReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @package com.igeek.boot.controller
 * @Description POI 报表数据实现类
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2024/1/10 23:21
 */
@RestController
@Api(tags = "报表接口")
@RequestMapping("/wisdom_factory/report")
public class ReportController {

    @Autowired
    private IReportService reportService;

    @ApiOperation("运营数据统计")
    @GetMapping("getBusinessReportData")
    public Result getBusinessReportData(){
        Map<String, Object>  data = reportService.selectBusinessData();
        return new Result(true , "查询成功" , data);
    }
    //获取到报表数据
    @ApiOperation("运营数据统计报表")
    @GetMapping("exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response){
        try{
            Map<String, Object> map = reportService.selectBusinessData();

            //取出返回数据，准备将报表数据写入Excel文件中
            String reportDate = (String) map.get("reportDate");
            Long totayInStockOrder = (Long) map.get("todayInStockOrder");
            Long todayIncomingStockOrder = (Long) map.get("todayIncomingStockOrder");
            Long thisWeekInStockOrder = (Long) map.get("thisWeekInStockOrder");
            Long thisWeekIncomingStockOrder = (Long) map.get("thisWeekIncomingStockOrder");
            Long totalInStock = (Long) map.get("totalInStock");
            Long totalIningStock = (Long) map.get("totalIningStock");
            Long todayOutStockOrder = (Long) map.get("todayOutStockOrder");
            Long todayOutgoingStockOrder = (Long) map.get("todayOutgoingStockOrder");
            Long thisWeekOutStockOrder = (Long) map.get("thisWeekOutStockOrder");
            Long thisWeekOutgoingStockOrder = (Long) map.get("thisWeekOutgoingStockOrder");
            Long totalOutStock = (Long) map.get("totalOutStock");
            Long totalOutgoingStock = (Long) map.get("totalOutgoingStock");
            List<Map> inList = (List<Map>) map.get("inList");
            List<Map> outList = (List<Map>) map.get("outList");

            //从类路径下加载报表文件
            ClassPathResource classPathResource = new ClassPathResource("/template/report_wms.xlsx");
            //创建excl实例  获取第一个sheet页
            XSSFWorkbook excel = new XSSFWorkbook(classPathResource.getInputStream());
            XSSFSheet sheet = excel.getSheetAt(0);

            //填充单元格数据
            XSSFRow row = sheet.getRow(2);
            row.getCell(5).setCellValue(reportDate);//日期

            row = sheet.getRow(4);
            row.getCell(5).setCellValue(totayInStockOrder);  //新增入库订单
            row.getCell(7).setCellValue(todayIncomingStockOrder);  //新增已入库订单

            row = sheet.getRow(5);
            row.getCell(5).setCellValue(thisWeekInStockOrder);  //本周系只能入库订单
            row.getCell(7).setCellValue(thisWeekIncomingStockOrder);//本周已入库订单

            row = sheet.getRow(6);
            row.getCell(5).setCellValue(totalInStock);//总入库订单
            row.getCell(7).setCellValue(totalIningStock);//总已入库订单

            row = sheet.getRow(8);
            row.getCell(5).setCellValue(todayOutStockOrder);  //今日出库订单
            row.getCell(7).setCellValue(todayOutgoingStockOrder);//今日已完成出库订单

            row = sheet.getRow(9);
            row.getCell(5).setCellValue(thisWeekOutStockOrder); //本周出库订单
            row.getCell(7).setCellValue(thisWeekOutgoingStockOrder);//本周已完成出库订单

            row = sheet.getRow(10);
            row.getCell(5).setCellValue(totalOutStock);//总出库订单
            row.getCell(7).setCellValue(totalOutgoingStock);//总已完成出库订单

            //热门入库物资
            int rowInNum = 13;
            for (Map inMap : inList) {
                String inName = (String) inMap.get("inName");
                Long inCount = ((BigDecimal) inMap.get("inCount")).longValue();
                BigDecimal ratio = (BigDecimal) inMap.get("ratio");
                row = sheet.getRow(rowInNum++);
                row.getCell(4).setCellValue(inName);
                row.getCell(5).setCellValue(inCount);
                row.getCell(6).setCellValue(ratio.doubleValue());
            }

            //热门出库物资
            int rowOutNum = 13 + inList.size() + 2;
            for (Map outMap : outList) {
                String outName = (String) outMap.get("outName");
                Long outCount = ((BigDecimal) outMap.get("outCount")).longValue();
                BigDecimal ratio = (BigDecimal) outMap.get("ratio");
                row = sheet.getRow(rowOutNum++);
                row.getCell(4).setCellValue(outName);
                row.getCell(5).setCellValue(outCount);
                row.getCell(6).setCellValue(ratio.doubleValue());
            }
            //通过输出流进行文件的下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            excel.write(out);
            out.flush();
            out.close();
            excel.close();
            return new Result(true,"报表导出成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false , "导出失败");
        }
    }
}
